import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
import qs from 'qs'


const service = axios.create({
    //   baseURL: process.env.VUE_APP_BASE_API,
    // baseURL: 'http://101.34.74.128:',
    // baseURL: 'http://172.16.0.170:7777/',
    baseURL: 'http://localhost:9999/',
    timeout: 5000
})

service.interceptors.request.use(
    config => {
        if (store.getters.token) {
            config.headers['token'] = getToken()
        }
        return config
    },
    error => {
        console.log(error) // for debug
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    response => {
        const res = response.data
        if (res.code == 401 || res.code == 404 || res.code == 400) {
            if (res.msg.indexOf('token') != -1) {
                Message({
                    message: 'Login expired, please log in again！',
                    type: 'error',
                    duration: 5 * 1000
                })
            } else {
                Message({
                    message: res.msg || 'Error',
                    type: 'error',
                    duration: 5 * 1000
                })
            }
        }
        return res
    },
    error => {
        console.log('err' + error) // for debug
        Message({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)


export function get(url, params) {
    return service.get(url, {
        params, // get 请求时带的参数
        timeout: 10000,
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        }
    })
}
export function del(url, params) {
    return service.delete(url, {
        params, // get 请求时带的参数
        timeout: 10000,
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        }
    })
}
export function delJSON(url, params) {
    return service.delete(url, {
        data: params,
        timeout: 10000,
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
        }
    })
}

// 自定义post
export function post(url, data) {
    return service.post(url, qs.stringify(data), {
        timeout: 10000,
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    })
}

// 自定义post
export function postJSON(url, data) {
    return service.post(url, data, {
        timeout: 10000,
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
        }
    })
}
// 自定义put
export function put(url, data) {
    return service.put(url, data, {
        timeout: 10000,
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
        }
    })
}

// 自定义patch
export function patch(url, data) {
    return service.patch(url, data, {
        timeout: 10000,
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
        }
    })
}

export default service
